/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

 */

#include "include/clipboardDialog.h"

clipboardDialog::clipboardDialog(Raptor *raptor, const QString &folder)
{
    r = raptor;

    setWindowTitle(tr("Clipboard"));

    topLabel = new QLabel(tr("Links Found:"));
    topLabel->setWordWrap(false);

    folderLabel = new QLabel(tr("Add selected links to folder:"));
    folderLabel->setWordWrap(false);

    checkPushButton = new QPushButton(tr("Uncheck All"));
    connect(checkPushButton, SIGNAL(clicked()), this, SLOT(slot_checkPushButton_clicked()));

    okPushButton = new QPushButton(tr("OK"));
    connect(okPushButton, SIGNAL(clicked()), this, SLOT(slot_okPushButton_clicked()));

    folderLineEdit = new QLineEdit(folder);

    treeView = new QTreeView;
    treeView->setMinimumSize(600, 400);

    modelFiles = new QStandardItemModel(0, 2);
    modelFiles->setHeaderData(0, Qt::Horizontal, tr("Add"));
    modelFiles->setHeaderData(1, Qt::Horizontal, tr("Link"));

    treeView->setModel(modelFiles);
    treeView->setAlternatingRowColors(true);
    treeView->setUniformRowHeights(true);
    treeView->setIconSize(QSize(24, 24));

    connect(modelFiles, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(slot_modelFiles_dataChanged(const QModelIndex &, const QModelIndex &)));

    spacer = new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

    QPointer<QGridLayout> layout = new QGridLayout;
    layout->addWidget(topLabel, 0, 0);
    layout->addWidget(treeView, 1, 0, 1, 5);
    layout->addWidget(checkPushButton, 2, 0);

    layout->addItem(spacer, 2, 1);

    layout->addWidget(folderLabel, 2, 2);
    layout->addWidget(folderLineEdit, 2, 3);
    layout->addWidget(okPushButton, 2, 4);

    setLayout(layout);
}

clipboardDialog::~clipboardDialog()
{
    if(topLabel) delete topLabel;
    if(folderLabel) delete folderLabel;
    if(checkPushButton) delete checkPushButton;
    if(okPushButton) delete okPushButton;
    if(treeView) delete treeView;
    if(modelFiles) delete modelFiles;
    if(folderLineEdit) delete folderLineEdit;

    delete spacer;
}

void clipboardDialog::setFolder(const QString &folder)
{
    if(folderLineEdit) folderLineEdit->setText(folder);
}

void clipboardDialog::syncLinks()
{
    //vllt gut, vllt auch nich
    //checkPushButton->setText(tr("Uncheck All"));

    modelFiles->removeRows(0, modelFiles->rowCount());

    QMap<QString, QPair<Qt::CheckState, bool> >::const_iterator it = clipboardLinks->constBegin();
    while(it != clipboardLinks->constEnd())
    {
	QList<QStandardItem *> listItem;

	//checked
	QStandardItem *item = new QStandardItem("");
	item->setCheckable(true);
	item->setEditable(false);
        item->setCheckState(it.value().first);
	listItem.append(item);

	//link
        QStandardItem *itemFilename = new QStandardItem(it.key());
	itemFilename->setEditable(false);
	listItem.append(itemFilename);

	modelFiles->appendRow(listItem);

        ++it;
    }
}

void clipboardDialog::slot_okPushButton_clicked()
{
    if(r)
    {
        for(int i=0; i<modelFiles->rowCount(); ++i)
        {
            if(modelFiles->item(i, 0)->data(Qt::CheckStateRole) ==  Qt::Checked)
            {
                if(folderLineEdit)
                    r->addWhenReady(folderLineEdit->text(), modelFiles->item(i, 1)->text());
                else
                    r->addWhenReady("New Folder", modelFiles->item(i, 1)->text());
            }
        }
    }

    clipboardLinks->clear();
    modelFiles->removeRows(0, modelFiles->rowCount());

    done(0);
}

void clipboardDialog::slot_checkPushButton_clicked()
{
    Qt::CheckState checkState = Qt::Checked;

    if(checkPushButton->text() == tr("Check All"))
    {
	checkState = Qt::Checked;
	checkPushButton->setText(tr("Uncheck All"));
    } else
    {
	checkState = Qt::Unchecked;
	checkPushButton->setText(tr("Check All"));
    }

    for(int i=0; i<modelFiles->rowCount(); ++i)
    {
	modelFiles->item(i,0)->setCheckState(checkState);
    }
}

void clipboardDialog::slot_modelFiles_dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight)
{
    if(modelFiles)
    {
        for(int i=topLeft.row(); i<=bottomRight.row(); ++i)
        {
            QString url = modelFiles->item(topLeft.row(), 1)->text();

            if(clipboardLinks->contains(url))
                clipboardLinks->insert(url, qMakePair(Qt::CheckState(modelFiles->data(topLeft, Qt::CheckStateRole).toInt()), clipboardLinks->value(url).second));
        }
    }
}
